﻿using FriendStar.Data;
using FriendStar.Domain.Entity.WareHouse;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FriendStar.Repository.Infrastructure
{
    public class ExecProcedure
    {
        /// <summary>
        /// 寄售库存操作
        /// </summary>
        /// <param name="UseUnit"></param>
        /// <param name="FAccountDate"></param>
        /// <param name="FDocuTypeNO"></param>
        /// <param name="FDocuNO"></param>
        /// <param name="FCurDate"></param>
        /// <param name="FHappenComNO"></param>
        /// <param name="FDocuID"></param>
        /// <param name="FDocuListID"></param>
        /// <param name="FProductionNO"></param>
        /// <param name="FQuantity"></param>
        /// <param name="FPrice"></param>
        /// <param name="FStoreNO"></param>
        /// <param name="FStoreSetNO"></param>
        /// <param name="FCreateBy"></param>
        /// <param name="FCredence"></param>
        /// <param name="FDescription"></param>
        /// <param name="FDescription2"></param>
        /// <param name="FFacNum"></param>
        /// <param name="FItemDesc"></param>
        /// <param name="DocumentNumType"></param>
        /// <param name="FDouBarCode"></param>
        /// <param name="Status">'11'出库新增 ‘12’ 出库修改 ‘13’ 出库删除  '21'入库新增 ‘22’ 入库修改 '23'入库删除    ‘31’废品更护新增 ‘32’废品更护修改 ‘33’废品更护删除   '41'盘存新增 ‘42' 盘存修改 ’43‘ 盘存删除 </param>
        /// <param name="DocNum"></param>
        /// <returns></returns>
        public static bool InOutStoJskStockCata(string UseUnit, string FAccountDate, string FDocuTypeNO, string FDocuNO, string FCurDate, string FHappenComNO, string FDocuID, string FDocuListID, string FProductionNO, decimal? FQuantity, decimal? FPrice, string FStoreNO, string FStoreSetNO, string FCreateBy, string FCredence, string FDescription, string FDescription2, string FFacNum, string FItemDesc, string DocumentNumType, string FDouBarCode, string Status, out string DocNum)
        {
            DocNum = "";
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                UseUnit = UseUnit,
                FAccountDate = FAccountDate,
                FDocuTypeNO = FDocuTypeNO,
                FDocuNO = FDocuNO,
                FCurDate = FCurDate,
                FHappenComNO = FHappenComNO,
                FDocuID = FDocuID,
                FDocuListID = FDocuListID,
                FProductionNO = FProductionNO,
                FQuantity = FQuantity,
                FPrice = FPrice,
                FStoreNO = FStoreNO,
                FStoreSetNO = FStoreSetNO,
                FCreateBy = FCreateBy,
                FCredence = FCredence,
                FDescription = FDescription,
                FDescription2 = FDescription2,
                FFacNum = FFacNum,
                FItemDesc = FItemDesc,
                DocumentNumType = DocumentNumType,
                FDouBarCode = FDouBarCode,
                Status = Status,
                DocNum = DocNum,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 2].Direction = System.Data.ParameterDirection.Output;
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoJskStockCata", para);
                    DocNum = para[para.Length - 2].Value.ToString();
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoWwkStockCataInsert(string UseUnit, WWK_41_ML_StoCPKConAccoEntity entity, WWK_42_MX_StoCPKListAccoEntity detailEntity, string FCreateBy, string FFacNum, string FItemDesc, string DocumentNumType, out string DocNum)
        {
            DocNum = "";
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                UseUnit = UseUnit,
                FAccountDate = entity.FAccountDate,
                FHappenComNO = entity.FHappenComNO,
                FDocuTypeNO = entity.FDocuTypeNO,
                FDocuNO = entity.FDocuNO,
                FCurDate = entity.FCurDate,
                FCredence = entity.FCredence,
                FDescription = entity.FDescription,

                FStoreNO = detailEntity.FStoreNO,
                FStoreSetNO = detailEntity.FStoreSetNO,
                FBatchNo = detailEntity.FBatchNo,
                FProCompanyNO = detailEntity.FProCompanyNO,
                FProductionNO = detailEntity.FProductionNO,
                FProductionTypeNO = detailEntity.FProductionTypeNO,
                FQuantity = detailEntity.FQuantity,
                FPrice = detailEntity.FPrice_KP,
                FContainerNO = detailEntity.FContainerNO,
                FContainerQuantity = detailEntity.FContainerQuantity,
                FContent = detailEntity.FContent,
                FFactContent = detailEntity.FFactContent,
                FPalletNO = detailEntity.FPalletNO,
                FPalletQuantity = detailEntity.FPalletQuantity,
                FPalletContent = detailEntity.FPalletContent,
                FPalletFactContent = detailEntity.FPalletFactContent,
                FDescription2 = detailEntity.FDescription,
                FSourceDouNO = detailEntity.FSourceDouNO,

                FCreateBy = FCreateBy,
                FFacNum = FFacNum,
                FItemDesc = FItemDesc,
                DocumentNumType = DocumentNumType,

                DocNum = DocNum,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 2].Direction = System.Data.ParameterDirection.Output;
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoWwkStockCata_Insert", para);
                    DocNum = para[para.Length - 2].Value.ToString();
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoWwkStockCataUpdate(string FDocuListID, decimal? FQuantity, decimal? FPrice, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,
                FQuantity = FQuantity,
                FPrice = FPrice,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoWwkStockCata_Update", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoWwkStockCataDelete(string FDocuListID, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoWwkStockCata_Delete", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoWwkStockCataCheck(string UseUnit, string FDocuNo, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                UseUnit = UseUnit,
                FDocuNo = FDocuNo,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoWwkStockCata_Check", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoWwkStockCataRevoke(string FDocuListID, string FCreateBy, string FFacNum, string FItemDesc, string DocumentNumType, out string DocNum)
        {
            DocNum = "";
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,

                FCreateBy = FCreateBy,
                FFacNum = FFacNum,
                FItemDesc = FItemDesc,
                DocumentNumType = DocumentNumType,

                DocNum = DocNum,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 2].Direction = System.Data.ParameterDirection.Output;
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoWwkStockCata_Revoke", para);
                    DocNum = para[para.Length - 2].Value.ToString();
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoYclkStockCataInsert(string UseUnit, CLK_41_ML_MaterialProConAccoEntity entity, CLK_42_MX_MaterialProListAccoEntity detailEntity, string FCreateBy, string FFacNum, string FItemDesc, string DocumentNumType, out string DocNum)
        {
            DocNum = "";
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                UseUnit = UseUnit,
                FAccountDate = entity.FAccountDate,
                FHappenComNO = entity.FHappenComNO,
                FDocuTypeNO = entity.FDocuTypeNO,
                FDocuNO = entity.FDocuNO,
                FCurDate = entity.FCurDate,
                FCredence = entity.FCredence,
                FCertificateNo = entity.FCertificateNo,
                FDescription = entity.FDescription,

                FStoreNO = detailEntity.FStoreNO,
                FStoreSetNO = detailEntity.FStoreSetNO,
                MaterialNO = detailEntity.MaterialNO,
                MaterialCode = detailEntity.MaterialCode,
                MaterialSizes = detailEntity.MaterialSizes,
                StandardNumber = detailEntity.StandardNumber,
                MaterialDemand = detailEntity.MaterialDemand,
                FQuantity_KP = detailEntity.FQuantity_KP,
                FQuantity = detailEntity.FQuantity,
                FPrice = detailEntity.FPrice,
                FMoney = detailEntity.FMoney,
                FZS_KP = detailEntity.FZS_KP,
                FZS = detailEntity.FZS,
                FConQuatity = detailEntity.FConQuatity,
                FSupplierNO = detailEntity.FSupplierNO,
                WagonNumber = detailEntity.WagonNumber,
                FOvenNum = detailEntity.FOvenNum,
                FBatchNo = detailEntity.FBatchNo,
                FDescription2 = detailEntity.FDescription,
                FSourceDouNO = detailEntity.FSourceDouNO,

                FCreateBy = FCreateBy,
                FFacNum = FFacNum,
                FItemDesc = FItemDesc,
                DocumentNumType = DocumentNumType,

                DocNum = DocNum,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 2].Direction = System.Data.ParameterDirection.Output;
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoYclkStockCata_Insert", para);
                    DocNum = para[para.Length - 2].Value.ToString();
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoYclkStockCataUpdate(string FDocuListID, decimal? FQuantity, decimal? FPrice, decimal? FZS, string FDescription, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,
                FQuantity = FQuantity,
                FPrice = FPrice,
                FZS = FZS,
                FDescription = FDescription,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoYclkStockCata_Update", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoYclkStockCataDelete(string FDocuListID, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoYclkStockCata_Delete", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoYclkStockCataCheck(string UseUnit, string FDocuNo, string FCreateBy)
        {
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                UseUnit = UseUnit,
                FDocuNo = FDocuNo,
                FCreateBy = FCreateBy,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoYclkStockCata_Check", para);
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool InOutStoYclkStockCataRevoke(string FDocuListID, string FCreateBy, string FFacNum, string FItemDesc, string DocumentNumType, out string DocNum)
        {
            DocNum = "";
            int resultState = 0;
            bool result = false;
            var obj = new
            {
                FDocuListID = FDocuListID,

                FCreateBy = FCreateBy,
                FFacNum = FFacNum,
                FItemDesc = FItemDesc,
                DocumentNumType = DocumentNumType,

                DocNum = DocNum,
                resultState = resultState
            };
            try
            {
                using (var db = new RepositoryBase())
                {
                    var para = SqlSugarTool.GetParameters(obj);
                    para[para.Length - 2].Direction = System.Data.ParameterDirection.Output;
                    para[para.Length - 1].Direction = System.Data.ParameterDirection.Output;
                    db.ExecuteStoredProcedure<string>("pro_InOutStoYclkStockCata_Revoke", para);
                    DocNum = para[para.Length - 2].Value.ToString();
                    result = Convert.ToInt32(para[para.Length - 1].Value) != 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
